package com.example.farmer.Activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.farmer.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.example.farmer.Fragment.Home.Seller_HomeFragment;
import com.example.farmer.Fragment.Add.addFragment;
import com.example.farmer.Fragment.Mine.MineFragment;
import com.example.farmer.Fragment.Message.MessageFragment;
import com.example.farmer.Fragment.CircleFragment;

public class Main2Activity extends AppCompatActivity {
    private static final String TAG = Main2Activity.class.getSimpleName();
    private Seller_HomeFragment mHomeFragment;
    private CircleFragment mCircleFragment;
    private MessageFragment mOrderFragment;
    private addFragment mAddFragment;
    private MineFragment mMineFragment;
    private BottomNavigationView bottomNavigationView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        // 初始化控件
        bottomNavigationView = findViewById(R.id.main2_bottom_nv);

        // 检查Intent，如果需要直接显示CircleFragment，则处理
        Intent intent = getIntent();
        if (intent != null && intent.getAction() != null) {
            // 检查Intent的action是否是要显示CircleFragment
            if (intent.getAction().equals(getPackageName() + ".ACTION_SHOW_CIRCLE_FRAGMENT")) {
                showCircleFragment();
            }
        }

        // main_bottom_nv tab点击切换
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            int position = item.getItemId() == R.id.shouye2 ? 0 :
                    item.getItemId() == R.id.Circle ? 1 :
                            item.getItemId() == R.id.xiaoxi2 ? 2 :
                                    item.getItemId() == R.id.wode2 ? 3 :
                                            4; // tianjia2
            selectedFragment(position);
            return true;
        });

        // 默认首页选中
        selectedFragment(0);
    }

    private void showCircleFragment() {
        if (findViewById(R.id.content) != null) {
            // 此处应确保CircleFragment尚未实例化，以避免内存泄漏
            if (mCircleFragment == null) {
                mCircleFragment = new CircleFragment();
                // 此处使用addToBackStack可返回前一个Fragment，但要注意管理栈的深度
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.content, mCircleFragment)
                        .addToBackStack(TAG)
                        .commit();
            }else {
                // 如果CircleFragment已经存在，直接显示
                getSupportFragmentManager()
                        .beginTransaction()
                        .show(mCircleFragment)
                        .commit();
            }
        }
    }

    public void selectedFragment(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);

        switch (position) {
            case 0:
                if (mHomeFragment == null) {
                    mHomeFragment = new Seller_HomeFragment();
                    transaction.add(R.id.content, mHomeFragment);
                } else {
                    transaction.show(mHomeFragment);
                }
                break;
            case 1:
                if (mCircleFragment == null) {
                    mCircleFragment = new CircleFragment();
                    transaction.add(R.id.content, mCircleFragment);
                } else {
                    transaction.show(mCircleFragment);
                }
                break;
            case 2:
                if (mOrderFragment == null) {
                    mOrderFragment = new MessageFragment();
                    transaction.add(R.id.content, mOrderFragment);
                } else {
                    transaction.show(mOrderFragment);
                }
                break;
            case 3:
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    transaction.add(R.id.content, mMineFragment);
                } else {
                    transaction.show(mMineFragment);
                }
                break;
            case 4:
                if (mAddFragment == null) {
                    mAddFragment = new addFragment();
                    transaction.add(R.id.content, mAddFragment);
                } else {
                    transaction.show(mAddFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mHomeFragment != null) {
            transaction.hide(mHomeFragment);
        }
        if (mCircleFragment != null) {
            transaction.hide(mCircleFragment);
        }
        if (mOrderFragment != null) {
            transaction.hide(mOrderFragment);
        }
        if (mMineFragment != null) {
            transaction.hide(mMineFragment);
        }
        if (mAddFragment != null) {
            transaction.hide(mAddFragment);
        }
    }
}